import axios from 'axios'
import router from '../router'
import { Notify } from 'vant';
//创建axios实例
const service = axios.create({
    baseURL: 'http://439744x0z6.zicp.vip',
    timeout: 5000,

})

// request拦截器
service.interceptors.request.use(config => {
  config.headers['Authorization'] = localStorage.getItem("token")
  
  config.headers['Content-Type'] = 'application/json'
  return config
})
//响应拦截器
service.interceptors.response.use(
    response=>{
        let res = response.data
        if(res.code === 200){
          return response
        }else{
          Notify({ type: 'warning', message: res.msg? '系统异常': res.msg });
          return Promise.reject(response.data.msg)
        }
    },error =>{
      if(error.response.data){
        error.massage = error.response.data.msg
      }
      if(error.response.status === 401){
        router.push('/login')
      }
      Notify({ type: 'warning', message: error.massage });
      return Promise.reject(error)
    }
)  

export default service